import com.credithc.eagleeye.util.FastJsonConvert;
import com.credithc.eagleeye.vo.RpcTraceInfoVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2019年12月04日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Slf4j
public class StatData {
    private ConcurrentHashMap<String, AtomicInteger> statDay = new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, ConcurrentHashMap<String,AtomicInteger>> conditionStatDay = new ConcurrentHashMap<>();
    @Test
    public void statCuishou() throws Exception {
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191204114317.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191204125942.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191008-1015.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191016-1023.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191024-1031.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191101-1105.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191106-1112.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191113-1119.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191120-1126.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191127-1203.json";
        //String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191204-1216.json";
        String fileName = "/Users/zhangjin/Work/credithc.com/日志系统/elasticsearch-dump-2.4.2/bin/export-cuishou-queryMyCaseListByPage-20191216-1222.json";
        InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
        BufferedReader br = new BufferedReader(reader);
        List<String> dateKeyList = new ArrayList<>();
        String str = null;
        // 定义计数器
        int i = 0;
        while ((str = br.readLine()) != null) {
            // i的值是从1开始
            i++;
            RpcTraceInfoVo rpcTraceInfoVo = FastJsonConvert.convertJSONToObject(str, RpcTraceInfoVo.class);
            String date = StringUtils.substring(rpcTraceInfoVo.getRequestDateTime(),0,10);
            AtomicInteger count = statDay.get(date);
            if (count == null) {
                count = new AtomicInteger(1);
                statDay.put(date, count);
                dateKeyList.add(date);
            }else{
                count.incrementAndGet();
            }
            ConcurrentHashMap<String, AtomicInteger> parameterMap = conditionStatDay.get(date);
            if (parameterMap == null) {
                parameterMap = new ConcurrentHashMap<>();
                conditionStatDay.put(date, parameterMap);
            }

            String json = StringUtils.replace(rpcTraceInfoVo.getRequestJson(), "\\\"", "\"");
            json= StringUtils.substringBetween(json, "parameter\":[\"", "\"]}");
            ParameterVo parameterVo = FastJsonConvert.convertJSONToObject(json, ParameterVo.class);
            Map<String,String> mapVo = FastJsonConvert.convertJSONToObject(json, Map.class);
            if (mapVo == null) {
                json = StringUtils.replace(json, "\\\"", "\"");
                json = StringUtils.replace(json, "\\\"", "");
                mapVo = FastJsonConvert.convertJSONToObject(json, Map.class);
                if (mapVo == null) {
                    System.out.println(json);
                    continue;
                }
            }
            for(Map.Entry<String,String> entry : mapVo.entrySet()) {
                String key = entry.getKey();
                String value = String.valueOf(entry.getValue());
                if (StringUtils.isNotBlank(value)) {
                    add(parameterMap, key);
                }
            }

            if (i % 10000==0) {
                System.out.println("已处理:"+i);
            }
        }
        reader.close();
        br.close();
        for(String strDate : dateKeyList) {
            System.out.println(strDate +" 总计:"+statDay.get(strDate)+"次");
            System.out.println(strDate +" 参数不为空统计:"+FastJsonConvert.convertObjectToJSON(conditionStatDay.get(strDate)));
        }
    }

    private void add(ConcurrentHashMap<String, AtomicInteger> parameterMap ,String paraName) {
        AtomicInteger count = parameterMap.get(paraName);
        if (count == null) {
            count = new AtomicInteger(1);
            parameterMap.putIfAbsent(paraName, count);
        }else{
            count.incrementAndGet();
        }
    }
}


@Data
class ParameterVo{
    private String contractNo;
    private String customerId;
    private String customerName;
    private String bucket;
    private String sourceChannels;
    private String product;
    private String cityName;
    private String onCollect;
    private String tel;
    private String tag;
    private String contractResult;
    private String promiseTimeStrFrom;
    private String promiseTimeStrTo;
    private String reminderFollowTimeStrFrom;
    private String reminderFollowTimeStrTo;
    private String assignTimeStrFrom;
    private String assignTimeStrTo;
    private String oaDateFrom;
    private String oaDateTo;
    private String endDateFrom;
    private String endDateTo;
    private String repairSuccTimeFrom;
    private String repairSuccTimeTo;
    private String isOrder;
    private String riskState;
    private String autoRepayStatus;
}
